<?php
header("content-type:text/html;charset=utf-8");
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class postsAction extends commonAction
{
    public function postsList(){
        $forumList=$this->getForumList();
        $this->assign('forumList',$forumList);
        $whereMap=array();
        if($_GET['posts_id']){
            $whereMap['id']=$_GET['posts_id'];
        }
        if($_GET['theme']){
            $whereMap['theme']=$_GET['theme'];
        }
        if($_GET['forum_id']){
            $whereMap['forum_id']=$_GET['forum_id'];
        }
        $count=D('posts')->where($whereMap)->count();
        list($pagesize,$offset,$pagestring)= pagestring($count, 5);
        $this->postsList=D('posts')->where($whereMap)->limit($offset,$pagesize)->order('dateline desc')->select();
        $this->assign('pagestring',$pagestring);
        $this->display();
    }
    public function postsGood(){
        $forumList=$this->getForumList();
        $this->assign('forumList',$forumList);
        $whereMap=array(
            'is_good'=>'yes'
        );
        if($_GET['posts-id']){
            $whereMap['id']=$_GET['posts-id'];
        }
        if($_GET['theme']){
            $whereMap['theme']=$_GET['theme'];
        }
        if($_GET['forum_id']){
            $whereMap['forum_id']=$_GET['forum_id'];
        }
        
        $count=M('posts')->where($whereMap)->count();
        list($pagesize,$offset,$pagestring)= pagestring($count, 5);
        $this->postsList=D('posts')->where($whereMap)->limit($offset,$pagesize)->order('dateline desc')->select();
        $this->assign('pagestring',$pagestring);
        $this->display();
    }
    public function goodPosts(){
        if($_GET){
            $whereMap=array('id'=>$_GET['id']);
            $this->incGood($_GET['forum_id']);
            $this->res=D('posts')->where($whereMap)->setField('is_good','yes');
            if($this->res){
                redirect($_GET['url']);
                exit();
            }
        }
    }
    public function cancelGood(){
        if($_GET){
            $whereMap=array('id'=>$_GET['id']);
            $this->res=D('posts')->where($whereMap)->setField('is_good','no');
            $this->decGood($_GET['forum_id']);
            if($this->res){
                redirect($_GET['url']);
                exit();
            }
        }
    }
     public function postsNew(){
        $forumList=$this->getForumList();
        $this->assign('forumList',$forumList);
        $whereMap=array(
            '_string'=>'date(dateline)=curdate()'
        );
        if($_GET['posts_id']){
            $whereMap['id']=$_GET['posts_id'];
        }
        if($_GET['theme']){
            $whereMap['theme']=$_GET['theme'];
        }
       if($_GET['forum_id']){
            $whereMap['forum_id']=$_GET['forum_id'];;
        }
        $count=M('posts')->where($whereMap)->count();
        list($pagesize,$offset,$pagestring)= pagestring($count, 5);
        $this->postsList=D('posts')->where($whereMap)->limit($offset,$pagesize)->order('dateline desc')->select();
        $this->assign('pagestring',$pagestring);
        $this->display();
    }
    public function postsDetail(){
        if($_GET['id']){
            $whereMap=array('id'=>$_GET['id']);
            $reply=$this->getReplyList($theme_id=$_GET['id']);
            $this->assign('replyList',$reply['replyList']);
            $this->assign('pagestring',$reply['pagestring']);
            $this->assign('replyCount',$reply['count']);
            $this->postsDetail=D('posts')->where($whereMap)->find();
            $this->display();
        }
    }
    public function postsDrop(){
        if($_GET['id']){
            $whereMap=array(
                'id'=>$_GET['id']
            );
            $this->res=D('posts')->where($whereMap)->delete();
            if($this->res){
                $this->assign('success',' 删除成功');
                redirect('postsList');
            }else{
                $this->assign('error',' 删除失败');
                redirect('postsList');
            }
        }
    }
    public function submitReply(){
        if($_POST){
            $data=array(
                'author_id'=>$_SESSION['userInfo']['id'],
                'author'=>$_SESSION['userInfo']['username'],
                'theme_id'=>$_POST['theme_id'],
                'content'=>$_POST['content'],
                'mood'=>$_POST['mood'],
                'dateline'=>date('Y-m-d H:i:s',time())
            );
            $res=D('reply')->data($data)->add();
            if($res){
                redirect("/Admin/posts/postsDetail?id={$_POST['theme_id']}");
                exit();
            }else{
                $this->assign('error',"<font color='red'>提交回复失败</font>");
                redirect("/Admin/posts/postsDetail?id={$_POST['theme_id']}");
                exit();
            }
        }
    }
   
}
?>
